package com.yc.thread.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 */
public class ReentrantLockTest4 {

    static final ReentrantLock lock = new ReentrantLock(true);
    static Condition condition = lock.newCondition();

    public static void main(String[] args) throws InterruptedException {

        for(int i=1;i<3;i++){
            new Thread(new ThreadDemo(i)).start();
        }

    }

    static class ThreadDemo implements Runnable {
        Integer id;

        public ThreadDemo(Integer id) {
            this.id = id;
        }

        @Override

        //
        public void run() {

            for(int i=0;i<3;i++){
                try {
                    lock.lock();
                    System.out.println("线程: "+id+" 获得锁");

//                    condition.signalAll();
//                    System.out.println("线程: "+id+" notifyAll");
//                    condition.await();
//                    System.out.println("线程: "+id+" 放弃锁");
//
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                    System.out.println("线程: "+id+" after wait...");
                } catch (Exception e) {

                } finally {
                    lock.unlock();
                }


            }
        }
    }

}

